<template>
	<view>
		<!-- #ifdef APP-PLUS -->
		<view>
			<uni-nav-bar left-icon="arrowleft" @clickLeft="back" title="找回密码" :statusBar='true' :fixed="true"></uni-nav-bar>
		</view>
		<!-- #endif -->
		<view class="retrieve-password">
			<view class="retrieve-password-ul">
				<view class="retrieve-password-li">
					<input type="text" v-model="phone" maxlength="11" placeholder="请输入手机号" placeholder-style="color:#AAAAAA" />
				</view>
				<view class="retrieve-password-li-two">
					<input type="text" v-model="messageCode" maxlength="4" placeholder="请输入验证码" placeholder-style="color:#AAAAAA" />
					<view class="xian"></view>
					<button style="color: #F02230;" @click="getSendSms">{{ getMessageText }}</button>
				</view>
				<view class="retrieve-password-li">
					<input type="password" v-model="pass" placeholder="请输入新密码（6-20位英文或数字）" placeholder-style="color:#AAAAAA" />
				</view>
			</view>
			<button class="retrieve-password-btn writetel-submitbtn-active" @click="confirmPs">确认修改</button>
			<view class="retrieve-password-tip" v-if="showPrise">已向{{ phone.replace(/^(\d{3})\d{4}(\d+)/,"$1****$2") }}发送验证码</view>
		</view>
	</view>
</template>
<script>
	import { config } from '@/utils/config.js';
	import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'
	export default {
		data() {
			return {
				phone: '',
				messageCode: '', //短信验证码
				pass: '',
				timer: null,
				countdown: 60,
				getMessageText: '发送验证码',
				showPrise: false
			}
		},
		components: {
			uniNavBar
		},
		methods: {
			back() {
				uni.navigateBack({})
			},
			beginSetTime() {
				if (this.countdown === 0) {
				  this.getMessageText = '发送验证码'
				  this.countdown = 60
				  this.timer = null
				  return
				} else {
				  this.getMessageText = `${this.countdown}s后重发`
				  this.countdown--
				}
				this.timer = setTimeout(() => {
				  this.beginSetTime()
				}, 1000)
			},
			getSendSms() {
				if (this.timer) {
					return false
				}
				if (!this.phone) {
					uni.showToast({
						icon: 'none',
						title: '请填写手机号'
					})
					return false
				}
				if (!(/^1[34578]\d{9}$/.test(this.phone))) {
					uni.showToast({
						title: '手机号码格式错误',
						icon: 'none'
					})
					return false
				}
				this.beginSetTime()
				uni.request({
					url: `${config.api_base_url_tools}sms`,
					method: 'POST',
					data: {
						sender: 'merchant',
						flag: 'findPassword',
						phone: this.phone
					},
					success: ((res) => {
						let { status, data } = res.data
						if (status.flag === true) {
							this.showPrise = true
							uni.showToast({
								icon: 'none',
								title: status.msg
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: status.msg
							})
						}
					}),
					fail: (() => {
						uni.showToast({
							icon: 'none',
							title: '发送失败, 网络异常'
						})
					})
				})
			},
			confirmPs() {
				if (!this.phone) {
					uni.showToast({
						icon: 'none',
						title: '请填写手机号'
					})
					return false
				}
				if (!this.messageCode) {
					uni.showToast({
						icon: 'none',
						title: '请填写验证码'
					})
					return false
				}
				if (!this.pass) {
					uni.showToast({
						icon: 'none',
						title: '请填写新密码'
					})
					return false
				}
				uni.request({
					url: `${config.api_base_url}merchant/store/findPassword/${this.messageCode}`,
					method: 'PUT',
					data: {
						'phone': this.phone,
						'password': this.pass
					},
					success: ((res) => {
						let { status, data } = res.data
						if (status.flag === true) {
							uni.showToast({
								icon: '找回密码成功'
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: status.msg
							})
						}
					}),
					fail: (() => {
						uni.showToast({
							icon: 'none',
							title: '找回密码失败, 网络异常'
						})
					})
				})
			}
		}
	}
</script>
<style>
	page {
		background: #F5F5F5;
	}
</style>
<style scoped>
	.retrieve-password {
		padding: 72rpx 46rpx;
		box-sizing: border-box;
	}

	.retrieve-password-ul {
		border: 2rpx solid rgba(221, 221, 221, 1);
		background: #fff;
		border-bottom: 0rpx;
	}

	.retrieve-password-li {
		padding: 0px 40rpx;
		box-sizing: border-box;
		height: 84rpx;
		border-bottom: 2rpx solid rgba(221, 221, 221, 1);
	}

	.retrieve-password-li input {
		font-size: 28rpx;
		height: 84rpx;
		line-height: 84rpx;
		color: #333;
	}

	.retrieve-password-li-two {
		padding: 0px 40rpx;
		box-sizing: border-box;
		height: 84rpx;
		border-bottom: 2rpx solid rgba(221, 221, 221, 1);
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.retrieve-password-li-two input {
		font-size: 28rpx;
		height: 84rpx;
		line-height: 84rpx;
		color: #333;
		float: left;
	}

	.retrieve-password-li-two button {
		font-size: 28rpx;
		height: 80rpx;
		line-height: 80rpx;

		background: #fff;
		float: right;
		margin: 0rpx;
		padding: 0rpx;
		max-width: 200rpx;
		font-weight: 400;
		min-width: 100rpx;
		text-align: center;
	}

	.blue {
		color: #1677FF;
	}

	.red {
		color: #F02230;
	}

	.retrieve-password-li-two button::after {
		border: none;
	}

	.xian {
		width: 2rpx;
		height: 22rpx;
		background: #979797;
		margin-right: -70rpx;
	}

	.retrieve-password-btn {
		width: 660rpx;
		height: 100rpx;
		background: rgba(0, 0, 0, 0.2);
		border-radius: 50rpx;
		border: 2rpx solid rgba(221, 221, 221, 1);
		font-size: 28rpx;
		line-height: 100rpx;
		color: rgba(255, 255, 255, 1);
		margin: 40rpx auto 26rpx;
		border: none;
	}

	.retrieve-password-btn::after {
		border: none
	}

	.retrieve-password-tip {
		font-size: 24rpx;
		height: 34rpx;
		line-height: 34rpx;
		text-align: center;
		color: rgba(170, 170, 170, 1);
	}

	.writetel-submitbtn-active {
		background-color: #F02230;
	}
</style>
